home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Fireworks 3 / Settings / Commands / Batch a Command....jsf next >
Encoding:
Text File  |  1999-11-19  |  3.2 KB  |  118 lines

  1.  
  2. // Copyright (c) 1998, 1999 Macromedia. All rights reserved.
  3.  
  4. fw.checkFwJsVersion(0);
  5.  
  6. var gBatchACommandIsAlreadyRunning;    // for this to work, you must not initialize this to a value here.
  7.  
  8. if (typeof(gBatchACommandIsAlreadyRunning) != "undefined") {
  9.  
  10.     // if this var is not undefined, we are trying to run this very
  11.     // command on itself -- naughty.
  12.     gBatchACommandIsAlreadyRunning = undefined;
  13.     alert("You may not run this command on itself.");
  14.     throw Errors.EUserCanceled;
  15.  
  16. } else {
  17.         
  18.     // OK, define this var so that we can test it for recursion.
  19.     // it doesn't really matter what we set the value to; we just
  20.     // have to ensure the value is not undefined.
  21.     gBatchACommandIsAlreadyRunning = true;
  22.     try {
  23.         
  24.         // initialize these up front, so that the "progress" dialog
  25.         // is forced to appear...
  26.         App.progressCountCurrent = 0;
  27.         App.progressCountTotal = 0;
  28.         App.batchStatusString = "";
  29.  
  30.         var theJsfToBatch = LocateJsfFileToBatch();
  31.         
  32.         if (theJsfToBatch != null) {
  33.             theDocList = App.chooseScriptTargetDialog(App.getPref("MultiFileBatchTypes"));
  34.             if (theDocList == null) {
  35.                 // The user must have canceled the "select files" dialog.
  36.             } else if (theDocList.length == 0) {
  37.                 // The user did something like "current files" when no files are open.
  38.                 alert(Errors.ENoFilesSelected);
  39.             } else {
  40.                 App.progressCountCurrent = 0;
  41.                 App.progressCountTotal = theDocList.length;
  42.                 for (var i = 0; i < theDocList.length; i++) {
  43.                     App.progressCountCurrent = i + 1;
  44.                     App.batchStatusString = "";
  45.                     ProcessOneDocPath(theDocList[i], theJsfToBatch);
  46.                 }    
  47.             }
  48.         }
  49.  
  50.     } catch (e) {
  51.  
  52.         // this resets this variable to being "undefined".
  53.         gBatchACommandIsAlreadyRunning = undefined;
  54.         
  55.         // propagate the exception on out.
  56.         throw e;
  57.  
  58.     }
  59.  
  60.     // this resets this variable to being "undefined".
  61.     gBatchACommandIsAlreadyRunning = undefined;
  62.  
  63. }
  64.  
  65. // ----------------------------------------------------------
  66. function LocateJsfFileToBatch()
  67. {
  68.     var kOpenLocPrefName = "LastOpenLocation";
  69.     var savePref = App.getPref(kOpenLocPrefName);
  70.     App.setPref(kOpenLocPrefName, App.appJsCommandsDir);
  71.     var theJsfToBatchArray = App.locateDocDialog(1, ["Fireworks JavaScript"]);
  72.     App.setPref(kOpenLocPrefName, savePref);
  73.     var theJsfToBatch = theJsfToBatchArray != null ? theJsfToBatchArray[0] : null;
  74.     return theJsfToBatch;
  75. }
  76.  
  77. // ----------------------------------------------------------
  78. function ProcessOneDocPath(docPathName, jsfPath)
  79. {
  80.     var theDocWasOpen = false;
  81.     var theDoc = App.findOpenDocument(docPathName);
  82.  
  83.     if (theDoc == null) {
  84.         theDoc = App.openDocument(docPathName, false);
  85.         theDocWasOpen = false;
  86.     } else {
  87.         theDocWasOpen = true;
  88.     }
  89.  
  90.     ProcessOneDoc(theDoc, jsfPath);
  91.  
  92.     if (theDoc != null && theDocWasOpen == false) {
  93.         // save and close, without prompting user
  94.         theDoc.save();
  95.         theDoc.close(false);
  96.     }
  97. }
  98.  
  99. // ----------------------------------------------------------
  100. function ProcessOneDoc(theDoc, jsfPath)
  101. {
  102.     var sourceDocumentPath = theDoc.filePathForSave;
  103.     if (sourceDocumentPath == null)
  104.         sourceDocumentPath = theDoc.filePathForRevert;
  105.     
  106.     if (sourceDocumentPath == null)
  107.         App.batchStatusString = "(untitled)";
  108.     else
  109.         App.batchStatusString = Files.getFilename(sourceDocumentPath.toString());
  110.  
  111.     fw.setActiveWindow(theDoc, true);
  112.  
  113.     theDoc.exitPaintMode();
  114.     theDoc.selectAll();
  115.  
  116.     fw.runScript(jsfPath);
  117. }
  118.